import requests
import re
import base64
from chaojiying_Python.chaojiying import Chaojiying_Client
# 1.获取验证图片
cookies = {
    "Cookie": "_passport_session=fe684fe23ec64779b1e67447baf29da30884; _passport_ct=b42406b3d0fc48e783c97d90dcc3f12et7754; RAIL_EXPIRATION=1584998130017; RAIL_DEVICEID=fhnevdhucUtHsKJ6THw8xV-5SB1kS-ccmU5CwetlAoqpJbjc7j4ItrrSOXu_Kx-y-5Ms88dZkqBWnK8iUaD8JnWGVV1xBBdJorTyVgIzGmSb48eGesCccwO4ome6DD96chXLVsC4mozhewdTfWtTeA7RYl1zhkTK; BIGipServerotn=619184394.24610.0000; BIGipServerpassport=803733770.50215.0000; route=495c805987d0f5c8c84b14f60212447d"}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"}

# 账号登录页面
url = "https://kyfw.12306.cn/passport/captcha/captcha-image64"
params = {"login_site": "E",
          "module": "login",
          "rand": "sjrand",
          "1584700481844": "",
          "callback": "jQuery19109161748489019864_1584700480155", #这些甚至都可以写死
          "_": "1584700480156"}
# res = requests.get(url, params=params, headers=headers)
# print(res.cookies)
session=requests.Session()
session.headers.update(headers)
res=session.get(url,params=params)
# 获取图片数据
data = res.content
img_base64 = re.findall(b'"image":"(.*?)"', data)[0]  # bs64数据
# print(img_base64)
img = base64.b64decode(img_base64)
with open("12306verify_img.jpg", "wb")as f:
    f.write(img)

chaojiying = Chaojiying_Client('cc9200cc', '091297', '12138')  # 用户中心>>软件ID 生成一个替换 96001
im = open('12306verify_img.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# print(chaojiying.PostPic(im, 9004))
answerr=chaojiying.PostPic(im, 9004)['pic_str'].replace('|','')# 将结果转换为需要的格式
print(answerr)

verify_url = "https://kyfw.12306.cn/passport/captcha/captcha-check"
params = {"callback": "jQuery19109161748489019864_1584700480155",
          "answer":answerr,
          "rand": "sjrand",
          "login_site": "E",
          "_": "1584700480156"}
# verify_res = requests.get(verify_url, params=params,headers=headers,cookies=cookies)
verify_res=session.get(verify_url,params=params)# 发送请求
print(verify_res.text)

